package io.bdmc.modules.bss.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.bdmc.common.utils.R;
import io.bdmc.common.utils.StringUtils;
import io.bdmc.common.utils.poi.ExcelUtil;
import io.bdmc.modules.bss.entity.*;
import io.bdmc.modules.bss.model.DeviceReportParam;
import io.bdmc.modules.bss.service.*;
import io.bdmc.modules.sys.controller.AbstractController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/bss/device/report")
public class DeviceReportController extends AbstractController {

    @Autowired
    IDeviceMachineFanService fanService;
    @Autowired
    IDeviceMachineEdfaService edfaService;
    @Autowired
    IDeviceDeviceService deviceService;
    @Autowired
    IDeviceMachineFecService fecService;
    @Autowired
    IDeviceMachineFspService fspService;
    @Autowired
    IDeviceMachineRtService rtService;
    @Autowired
    IDeviceMachineRfaService rfaService;
    @Autowired
    IDeviceMachineOpmService opmService;
    @Autowired
    IDeviceMachineOpmModuleService opmModuleService;
    @Autowired
    IDeviceMachineOsService osService;
    @Autowired
    IDeviceMachineOsModuleService osModuleService;

    //导出报表
    @GetMapping("export")
    public R getReport(DeviceReportParam reportParam) {
        final DeviceDevice device = deviceService.getDeviceBySn(reportParam.getDeviceSn());
        String filePath = "";
        switch (device.getDeviceType()) {
            case "FAN":
                QueryWrapper<DeviceMachineFan> w1 = new QueryWrapper<DeviceMachineFan>().eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w1.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w1.lt("create_time", reportParam.getEndTime());
                List<DeviceMachineFan> fanList = fanService.list(w1);
                ExcelUtil<DeviceMachineFan> util = new ExcelUtil<>(DeviceMachineFan.class);
                filePath = util.exportExcel(fanList, device.getDeviceName() + "HistoryData");
                break;
            case "EDFA":
                QueryWrapper<DeviceMachineEdfa> w2 = new QueryWrapper<DeviceMachineEdfa>().eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w2.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w2.lt("create_time", reportParam.getEndTime());
                List<DeviceMachineEdfa> edfaList = edfaService.list(w2);
                ExcelUtil<DeviceMachineEdfa> edfaUtil = new ExcelUtil<>(DeviceMachineEdfa.class);
                filePath = edfaUtil.exportExcel(edfaList, device.getDeviceName() + "HistoryData");
                break;
            case "FEC10":
            case "FEC64":
                QueryWrapper<DeviceMachineFec> w3 = new QueryWrapper<DeviceMachineFec>().eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w3.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w3.lt("create_time", reportParam.getEndTime());
                List<DeviceMachineFec> fecList = fecService.list(w3);
                ExcelUtil<DeviceMachineFec> fecUtil = new ExcelUtil<>(DeviceMachineFec.class);
                filePath = fecUtil.exportExcel(fecList, device.getDeviceName() + "HistoryData");
                break;
            case "FSP6":
            case "FSP8":
                QueryWrapper<DeviceMachineFsp> w4 = new QueryWrapper<DeviceMachineFsp>().eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w4.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w4.lt("create_time", reportParam.getEndTime());
                List<DeviceMachineFsp> fspList = fspService.list(w4);
                ExcelUtil<DeviceMachineFsp> fspUtil = new ExcelUtil<>(DeviceMachineFsp.class);
                filePath = fspUtil.exportExcel(fspList, device.getDeviceName() + "HistoryData");
                break;
            case "RFA":
                QueryWrapper<DeviceMachineRfa> w5 = new QueryWrapper<DeviceMachineRfa>().eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w5.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w5.lt("create_time", reportParam.getEndTime());
                List<DeviceMachineRfa> rfaList = rfaService.list(w5);
                ExcelUtil<DeviceMachineRfa> rfaUtil = new ExcelUtil<>(DeviceMachineRfa.class);
                filePath = rfaUtil.exportExcel(rfaList, device.getDeviceName() + "HistoryData");
                break;
            case "RT":
                QueryWrapper<DeviceMachineRt> w6 = new QueryWrapper<DeviceMachineRt>().eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w6.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w6.lt("create_time", reportParam.getEndTime());
                List<DeviceMachineRt> rtList = rtService.list(w6);
                ExcelUtil<DeviceMachineRt> rtUtil = new ExcelUtil<>(DeviceMachineRt.class);
                filePath = rtUtil.exportExcel(rtList, device.getDeviceName() + "HistoryData");
                break;
            case "OPM":
                QueryWrapper<DeviceMachineOpm> w7 = new QueryWrapper<DeviceMachineOpm>().eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w7.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w7.lt("create_time", reportParam.getEndTime());
                List<DeviceMachineOpm> opmList = opmService.list(w7);
                int opmPowerIndexCount = 0;
                for (DeviceMachineOpm deviceMachineOpm : opmList) {
                    final List<DeviceMachineOpmModule> opmModuleList = opmModuleService.selectByMachineOpmId(deviceMachineOpm.getMachineOpmId());
                    deviceMachineOpm.setOpmModuleList(opmModuleList);
                    if (opmModuleList != null && opmPowerIndexCount == 0) {
                        opmPowerIndexCount = opmModuleList.size();
                    }
                }
                ExcelUtil<DeviceMachineOpm> opmUtil = new ExcelUtil<>(DeviceMachineOpm.class);
                filePath = opmUtil.exportExcel(opmList, device.getDeviceName() + "HistoryData", opmPowerIndexCount);
                break;
            case "OS":
                QueryWrapper<DeviceMachineOs> w8 = new QueryWrapper<DeviceMachineOs>().eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w8.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w8.lt("create_time", reportParam.getEndTime());
                List<DeviceMachineOs> osList = osService.list(w8);
                int powerIndexCount = 0;
                for (DeviceMachineOs deviceMachineOpm : osList) {
                    final List<DeviceMachineOsModule> osModuleList = osModuleService.selectByMachineOsId(deviceMachineOpm.getMachineOsId());
                    deviceMachineOpm.setOsModuleList(osModuleList);
                    if (osModuleList != null && powerIndexCount == 0) {
                        powerIndexCount = osModuleList.size();
                    }
                }
                ExcelUtil<DeviceMachineOs> osUtil = new ExcelUtil<>(DeviceMachineOs.class);
                filePath = osUtil.exportExcel(osList, device.getDeviceName() + "HistoryData", powerIndexCount);
                break;
            default:
                break;
        }
        return R.ok().putData(filePath);
    }
}
